#include "filelistmodel.h"
#include "msgprint.h"

FileListModel::FileListModel(QObject *parent) :
    QAbstractTableModel(parent)
{
    m_dataList.clear();
}

int FileListModel::rowCount(const QModelIndex &) const
{
    return m_dataList.count();
}

int FileListModel::columnCount(const QModelIndex &) const
{
    return Convert::FileInfoItem_Count;
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()){
        if ((role == Qt::DisplayRole))
        {
            switch(index.column()){
            case Convert::FileInfo_name:{
                return QString(m_dataList.at(index.row()).fileName);
            }
            case Convert::FileInfo_duration:{
                return QString(m_dataList.at(index.row()).duration);
            }
            case Convert::FileInfo_outFormat:{
                return QString(m_dataList.at(index.row()).outFormat);
            }
            case Convert::FileInfo_outSize:{
                return QString(m_dataList.at(index.row()).outSize);
            }
            case Convert::FileInfo_outName:{
                return QString(m_dataList.at(index.row()).outName);
            }
            case Convert::FileInfo_status:{
                // TODO: get status
                return QString("not start");
            }
            default:{
                return QVariant();
            }
            }
        }else if (role == Qt::EditRole && index.column() == Convert::FileInfo_outName ){
            return QString(m_dataList.at(index.row()).outName);
        }
    }
    return QVariant();
}

QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole)
    {
        if (orientation == Qt::Horizontal) {
            switch (section)
            {
            case Convert::FileInfo_name:
                return QString(tr("Name"));
            case Convert::FileInfo_duration:
                return QString(tr("Duration"));
            case Convert::FileInfo_outFormat:
                return QString(tr("Target"));
            case Convert::FileInfo_outSize:
                return QString(tr("Output size"));
            case Convert::FileInfo_outName:
                return QString(tr("Output file"));
            case Convert::FileInfo_status:
                return QString(tr("Status"));
            }
        }
    }
    return QVariant();
}

void FileListModel::addFile(const Convert::FileInfo &info)
{
    beginInsertRows(QModelIndex(), m_dataList.count(), m_dataList.count());
    m_dataList.append(info);
    endInsertRows();
}

Qt::ItemFlags FileListModel::flags(const QModelIndex & index) const
{
    if (index.column()==Convert::FileInfo_outName){
        return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
    }
    return QAbstractTableModel::flags(index);
}

bool FileListModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    if (index.isValid() && role == Qt::EditRole && index.column()==Convert::FileInfo_outName)
    {
        m_dataList[index.row()].outName = value.toString();
    }
    return true;
}

static bool indexLessThan(const QModelIndex &s1, const QModelIndex &s2)
{
    return s1.row()>s2.row();
}

void FileListModel::removeRowsByModelIndexList(const QModelIndexList&indexlist)
{
    QModelIndexList indexs = indexlist;
    if (indexs.count()){
        int row;
        qSort(indexs.begin(), indexs.end(), indexLessThan);
        for (int i=0;i<indexs.count();i++){
            row = indexs.at(i).row();
            beginRemoveRows(QModelIndex(), row, row);
            m_dataList.removeAt(row);
            endRemoveRows();
        }
    }else{
        ERRR() << "indexs.count==0";
    }
}
